
function Annotation(id,x,y,content){
	
	if(arguments.callee.hash.hasOwnProperty(id)){
		return arguments.callee.hash[id];
	}
	
	if(!id){
		var id=-1;
		while(!arguments.callee.hash.hasOwnProperty(id)){
			id = parseInt(Math.random()*1000);
		}
	}
	
	if(!x){ x = parseInt(Math.random()*500); }
	if(!y){ y = parseInt(Math.random()*500); }
	
	this.id = id;
	
	var dom = new compono.DOMBuilder(null,{core:this});
		dom.DIV(
			{'class':'annotation','style':('top:'+y+"px;"+'left:'+x+"px;")},
			dom.A({'class':'anchor','href':'javascript:void(0)'}),
			dom.DIV(
				{'class':'window'},
				dom.DIV(
					{'class':'body'},
					dom.DIV({'class':'output'}),
					dom.TEXTAREA({'class':'edit'})
				),
				dom.DIV(
					{'class':'footer'},
					dom.A("Expand",{'class':'expand','href':'javascript:void(0)'}),
					dom.DIV(
						{'class':'controls'},
						dom.A("Save",{'class':'save','href':'javascript:void(0)'}),
						dom.A("Cancel",{'class':'cancel','href':'javascript:void(0)'})
					)
				)
			)
		)

	var h = dom.render();

	compono.Event.add("mousedown",function(e){
		e.stopPropagation();
	},h.window)
	
	compono.Event.add("ESQ",function(){
		this.core.cancel();
		this.core.hide();
	},h.window)

	
	var dd = new compono.DragnDrop(h,null,function(){return this.anchor;}).run();
	dd.onDrag=function(o,e){
		e.preventDefault();
	}
	dd.onMove=function(o,e){
		o.moving=true;
	}

	
	
	compono.Event.add("click",function(e){
		if(this.parentNode.moving){
			this.parentNode.moving = false;
			return;
		}
		if(this.core.open){
			this.core.hide();
		}else{
			this.core.show();
		}
	},h.anchor)
	
	compono.Event.add("ENTER",function(e){
		this.core.show(e,this);
	},h.anchor)
	
	h.anchor.innerHTML = arguments.callee.hash.length + 1;
	
	
	compono.Event.add("dblclick",function(e){
		this.core.edit(e,this);
	},h.window.body.output)

	
	compono.Event.add("CTRL+S",function(e){
		this.core.save(e,this);
		e.preventDefault();
	},h.window.body.edit)
	
	compono.Event.add("click",function(e){
		this.core.save(e,this);
	},h.window.footer.controls.save)
	
	compono.Event.add("click",function(e){
		this.core.cancel(e,this);
	},h.window.footer.controls.cancel)
	

	var ff = new compono.DragnDrop(h.window.footer.expand).run();
	ff.onDrag=function(o,e){
		o.x = e.clientX;
		o.y = e.clientY;
		o.content = o.core.editmode ? o.core.html.window.body.edit : o.core.html.window.body.output
		o.w = o.content.offsetWidth;
		o.h = o.content.offsetHeight;
		
		e.preventDefault();
		e.stopPropagation();
		return false;
	}
	ff.onMove=function(o,e){
		o.content.style.width = (e.clientX - o.x + o.w) + "px";
		o.content.style.height = (e.clientY - o.y + o.h) + "px";
		return false;
	}
	
	
	content = content || "**bold**, ''italic'', __underline__, --strikethrough--, !!hightlight!!, //selection//, [[link|link]], {{label|formControlNameOrId}}";
	
	this.text = content;
	this.htmlText = Wiki.render(content);
	h.window.body.edit.value = this.text;
	h.window.body.output.innerHTML = this.htmlText;
	
	arguments.callee.hash[id] = this;
	arguments.callee.hash.length++;
	
	this.html = h;
	
}

Annotation.hash = {
	length:0
}


Annotation.prototype = {
	constructor:Annotation,
	id:0,
	html:{},
	open:false,
	text:"",
	htmlText:"",
	editmode:false,
	show:function(){
		this.html.window.style.display = "block";
		this.html.setAttribute("open","true");
		this.open = true;
		this.documentDisplay = new Function("e","arguments.callee.scope.cancel();arguments.callee.scope.hide();");
		this.documentDisplay.scope = this;
		document.addEventListener("mousedown",this.documentDisplay,false);
		return this;
	},
	hide:function(){
		this.html.window.style.display = "none";
		this.html.setAttribute("open","false");
		this.open = false;
		if(typeof(this.documentDisplay)=="function"){
			document.removeEventListener("mousedown",this.documentDisplay,false);
		}
		return this;
	},
	edit:function(e,o){
		if(o){
			this.html.window.body.edit.style.width = o.offsetWidth + "px";
			this.html.window.body.edit.style.height = o.offsetHeight + "px";
		}
		if(this.html.window.body.edit.value){
			this.html.window.body.edit.oldValue = this.html.window.body.edit.value;
		}
		this.html.window.footer.controls.style.display = "block";
		this.html.setAttribute("editmode","true");
		this.editmode = true;
	},
	copy:function(){},
	move:function(x,y){
		this.html.style.top = (x) + "px";
		this.html.style.left = (y) + "px";
	},
	save:function(e,o){
		this.text = this.html.window.body.edit.value;
		this.htmlText = Wiki.render(this.text);
		this.html.window.body.output.innerHTML = this.htmlText;
		this.html.setAttribute("editmode","false");
		this.html.window.footer.controls.style.display = "none";
		this.editmode = false;
	},
	cancel:function(){
		if(this.html.window.body.edit.oldValue){
			this.text = this.html.window.body.edit.oldValue;
			this.html.window.body.edit.value = this.text;
		}
		this.html.window.footer.controls.style.display = "none";
		this.html.setAttribute("editmode","false");
		this.editmode = false;
	}
}



